home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 14 / CU Amiga Magazine's Super CD-ROM 14 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-09].iso / CUCD / Programming / Mesa-2.2 / demos / offset.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-29  |  8.2 KB  |  321 lines

  1. /****************************************************************************
  2. Copyright 1995 by Silicon Graphics Incorporated, Mountain View, California.
  3.  
  4.                         All Rights Reserved
  5.  
  6. Permission to use, copy, modify, and distribute this software and its 
  7. documentation for any purpose and without fee is hereby granted, 
  8. provided that the above copyright notice appear in all copies and that
  9. both that copyright notice and this permission notice appear in 
  10. supporting documentation, and that the name of Silicon Graphics not be
  11. used in advertising or publicity pertaining to distribution of the
  12. software without specific, written prior permission.  
  13.  
  14. SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15. INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  16. EVENT SHALL SILICON GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17. CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  18. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  19. OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  20. PERFORMANCE OF THIS SOFTWARE.
  21.  
  22. ****************************************************************************/
  23.  
  24. /*
  25.  * Derived from code written by Kurt Akeley, November 1992
  26.  *
  27.  *    Uses PolygonOffset to draw hidden-line images.  PolygonOffset
  28.  *        shifts the z values of polygons an amount that is
  29.  *        proportional to their slope in screen z.  This keeps
  30.  *        the lines, which are drawn without displacement, from
  31.  *        interacting with their respective polygons, and
  32.  *        thus eliminates line dropouts.
  33.  *
  34.  *    The left image shows an ordinary antialiased wireframe image.
  35.  *    The center image shows an antialiased hidden-line image without
  36.  *        PolygonOffset.
  37.  *    The right image shows an antialiased hidden-line image using
  38.  *        PolygonOffset to reduce artifacts.
  39.  *
  40.  *    Drag with a mouse button pressed to rotate the models.
  41.  *    Press the escape key to exit.
  42.  */
  43.  
  44. /*
  45.  * Modified for OpenGL 1.1 glPolygonOffset() conventions
  46.  */
  47.  
  48.  
  49. #include <GL/glx.h>
  50. #include <GL/glu.h>
  51. #include <X11/keysym.h>
  52. #include <stdlib.h>
  53. #include <stdio.h>
  54. #include <string.h>
  55.  
  56. /*#undef GL_EXT_polygon_offset   uncomment to use new version*/
  57.  
  58.  
  59. #ifndef EXIT_FAILURE
  60. #  define EXIT_FAILURE    1
  61. #endif
  62. #ifndef EXIT_SUCCESS
  63. #  define EXIT_SUCCESS    0
  64. #endif
  65.  
  66. #define MAXQUAD 6
  67.  
  68. typedef float Vertex[3];
  69.  
  70. typedef Vertex Quad[4];
  71.  
  72. /* data to define the six faces of a unit cube */
  73. Quad quads[MAXQUAD] = {
  74.     0,0,0, 1,0,0, 1,1,0, 0,1,0,
  75.     0,0,1, 1,0,1, 1,1,1, 0,1,1,
  76.     0,0,0, 1,0,0, 1,0,1, 0,0,1,
  77.     0,1,0, 1,1,0, 1,1,1, 0,1,1,
  78.     0,0,0, 0,0,1, 0,1,1, 0,1,0,
  79.     1,0,0, 1,0,1, 1,1,1, 1,1,0
  80. };
  81.  
  82. #define WIREFRAME    0
  83. #define HIDDEN_LINE    1
  84.  
  85. static void error(const char* prog, const char* msg);
  86. static void cubes(int mx, int my, int mode);
  87. static void fill(Quad quad);
  88. static void outline(Quad quad);
  89. static void draw_hidden(Quad quad, int mode);
  90. static void process_input(Display *dpy, Window win);
  91. static int query_extension(char* extName);
  92.  
  93. static int attributeList[] = { GLX_RGBA, GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1,
  94.     GLX_BLUE_SIZE, 1, GLX_DOUBLEBUFFER, GLX_DEPTH_SIZE, 1, None };
  95.  
  96. static int dimension = 3;
  97.  
  98. int main(int argc, char** argv) {
  99.     Display *dpy;
  100.     XVisualInfo *vi;
  101.     XSetWindowAttributes swa;
  102.     Window win;
  103.     GLXContext cx;
  104.  
  105.     dpy = XOpenDisplay(0);
  106.     if (!dpy) error(argv[0], "can't open display");
  107.  
  108.     vi = glXChooseVisual(dpy, DefaultScreen(dpy), attributeList);
  109.     if (!vi) error(argv[0], "no suitable visual");
  110.  
  111.     cx = glXCreateContext(dpy, vi, 0, GL_TRUE);
  112.  
  113.     swa.colormap = XCreateColormap(dpy, RootWindow(dpy, vi->screen),
  114.                                    vi->visual, AllocNone);
  115.  
  116.     swa.border_pixel = 0;
  117.     swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask |
  118.     ButtonPressMask | ButtonMotionMask;
  119.     win = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 0, 0, 900, 300,
  120.             0, vi->depth, InputOutput, vi->visual,
  121.             CWBorderPixel|CWColormap|CWEventMask, &swa);
  122.     XStoreName(dpy, win, "hiddenline");
  123.     XMapWindow(dpy, win);
  124.  
  125.     glXMakeCurrent(dpy, win, cx);
  126.  
  127.     /* check for the polygon offset extension */
  128. #ifndef GL_VERSION_1_1
  129.     if (!query_extension("GL_EXT_polygon_offset"))
  130.         error(argv[0], "polygon_offset extension is not available");
  131. #endif
  132.  
  133.     /* set up viewing parameters */
  134.     glMatrixMode(GL_PROJECTION);
  135.     gluPerspective(20, 1, 0.1, 20);
  136.     glMatrixMode(GL_MODELVIEW);
  137.     glTranslatef(0, 0, -15);
  138.  
  139.     /* set other relevant state information */
  140.     glEnable(GL_DEPTH_TEST);
  141. #ifdef GL_EXT_polygon_offset
  142.     printf("using 1.0 offset extension\n");
  143.     glPolygonOffsetEXT( 1.0, 0.00001 );
  144. #else
  145.     printf("using 1.1 offset\n");
  146.     glPolygonOffset( 1.0, 0.5 );
  147. #endif
  148.  
  149.     glShadeModel( GL_FLAT );
  150.     glDisable( GL_DITHER );
  151.  
  152.     /* process events until the user presses ESC */
  153.     while (1) process_input(dpy, win);
  154.  
  155.     return 0;
  156. }
  157.  
  158. static void
  159. draw_scene(int mx, int my) {
  160.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  161.  
  162.     glPushMatrix();
  163.     glTranslatef(-1.7, 0.0, 0.0);
  164.     cubes(mx, my, WIREFRAME);
  165.     glPopMatrix();
  166.  
  167.     glPushMatrix();
  168.     cubes(mx, my, HIDDEN_LINE);
  169.     glPopMatrix();
  170.  
  171.     glPushMatrix();
  172.     glTranslatef(1.7, 0.0, 0.0);
  173. #ifdef GL_EXT_polygon_offset
  174.     glEnable(GL_POLYGON_OFFSET_EXT);
  175. #else
  176.     glEnable(GL_POLYGON_OFFSET_FILL);
  177. #endif
  178.     cubes(mx, my, HIDDEN_LINE);
  179. #ifdef GL_EXT_polygon_offset
  180.     glDisable(GL_POLYGON_OFFSET_EXT);
  181. #else
  182.     glDisable(GL_POLYGON_OFFSET_FILL);
  183. #endif
  184.     glPopMatrix();
  185. }
  186.  
  187.  
  188. static void
  189. cubes(int mx, int my, int mode) {
  190.     int x, y, z, i;
  191.  
  192.     /* track the mouse */
  193.     glRotatef(mx / 2.0, 0, 1, 0);
  194.     glRotatef(my / 2.0, 1, 0, 0);
  195.  
  196.     /* draw the lines as hidden polygons */
  197.     glTranslatef(-0.5, -0.5, -0.5);
  198.     glScalef(1.0/dimension, 1.0/dimension, 1.0/dimension);
  199.     for (z = 0; z < dimension; z++) {
  200.     for (y = 0; y < dimension; y++) {
  201.         for (x = 0; x < dimension; x++) {
  202.         glPushMatrix();
  203.         glTranslatef(x, y, z);
  204.         glScalef(0.8, 0.8, 0.8);
  205.         for (i = 0; i < MAXQUAD; i++)
  206.             draw_hidden(quads[i], mode);
  207.         glPopMatrix();
  208.         }
  209.     }
  210.     }
  211. }
  212.  
  213. static void
  214. fill(Quad quad) {
  215.     /* draw a filled polygon */
  216.     glBegin(GL_QUADS);
  217.     glVertex3fv(quad[0]);
  218.     glVertex3fv(quad[1]);
  219.     glVertex3fv(quad[2]);
  220.     glVertex3fv(quad[3]);
  221.     glEnd();
  222. }
  223.  
  224. static void
  225. outline(Quad quad) {
  226.     /* draw an outlined polygon */
  227.     glBegin(GL_LINE_LOOP);
  228.     glVertex3fv(quad[0]);
  229.     glVertex3fv(quad[1]);
  230.     glVertex3fv(quad[2]);
  231.     glVertex3fv(quad[3]);
  232.     glEnd();
  233. }
  234.  
  235. static void
  236. draw_hidden(Quad quad, int mode) {
  237.     if (mode == HIDDEN_LINE) {
  238.     glColor3f(0, 0, 0);
  239.     fill(quad);
  240.     }
  241.  
  242.     /* draw the outline using white, optionally fill the interior with black */
  243.     glColor3f(1, 1, 1);
  244.     outline(quad);
  245. }
  246.  
  247. static void
  248. process_input(Display *dpy, Window win) {
  249.     XEvent event;
  250.     static int prevx, prevy;
  251.     static int deltax = 90, deltay = 40;
  252.  
  253.     do {
  254.     char buf[31];
  255.     KeySym keysym;
  256.  
  257.     XNextEvent(dpy, &event);
  258.     switch(event.type) {
  259.     case Expose:
  260.         break;
  261.     case ConfigureNotify: {
  262.         /* this approach preserves a 1:1 viewport aspect ratio */
  263.         int vX, vY, vW, vH;
  264.         int eW = event.xconfigure.width, eH = event.xconfigure.height;
  265.         if (eW >= eH) {
  266.         vX = 0;
  267.         vY = (eH - eW) >> 1;
  268.         vW = vH = eW;
  269.         } else {
  270.         vX = (eW - eH) >> 1;
  271.         vY = 0;
  272.         vW = vH = eH;
  273.         }
  274.         glViewport(vX, vY, vW, vH);
  275.         }
  276.         break;
  277.     case KeyPress:
  278.         (void) XLookupString(&event.xkey, buf, sizeof(buf), &keysym, NULL);
  279.         switch (keysym) {
  280.         case XK_Escape:
  281.         exit(EXIT_SUCCESS);
  282.         default:
  283.         break;
  284.         }
  285.     case ButtonPress:
  286.         prevx = event.xbutton.x;
  287.         prevy = event.xbutton.y;
  288.         break;
  289.     case MotionNotify:
  290.         deltax += (event.xbutton.x - prevx); prevx = event.xbutton.x;
  291.         deltay += (event.xbutton.y - prevy); prevy = event.xbutton.y;
  292.         break;
  293.     default:
  294.         break;
  295.     }
  296.     } while (XPending(dpy));
  297.  
  298.     draw_scene(deltax, deltay);
  299.     glXSwapBuffers(dpy, win);
  300. }
  301.  
  302. static void
  303. error(const char *prog, const char *msg) {
  304.     fprintf(stderr, "%s: %s\n", prog, msg);
  305.     exit(EXIT_FAILURE);
  306. }
  307.  
  308. static int
  309. query_extension(char* extName) {
  310.     char *p = (char *) glGetString(GL_EXTENSIONS);
  311.     char *end = p + strlen(p);
  312.     while (p < end) {
  313.         int n = strcspn(p, " ");
  314.         if ((strlen(extName) == n) && (strncmp(extName, p, n) == 0))
  315.             return GL_TRUE;
  316.         p += (n + 1);
  317.     }
  318.     return GL_FALSE;
  319. }
  320.  
  321.